package base.consumer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

public class ConsumerTest {

    public void test(Consumer<Integer> consumer) {
        consumer.accept(100);
    }

    public static void main(String[] args) {
        ConsumerTest consumerTest = new ConsumerTest();

        Consumer<Integer> consumer = System.out::println;
        IntConsumer intConsumer = System.out::println;

        //面向对象式的对象传递
        consumerTest.test(consumer);
        //函数式 方式，传递行为--等价于 consumerTest.test(System.out::println);
        consumerTest.test(intConsumer::accept);

        packagePerson();


    }

    public static void packagePerson() {

        List<Person> lisiList = new ArrayList<>();
        Consumer<List<Person>> consumer = x -> x.forEach(one -> {
            if (one.getName().equals("lisi")) {
                lisiList.add(one);
            }
        });
        consumer = consumer.andThen(x -> x.forEach(one -> lisiList.removeIf(p -> p.getAge() < 23)));


        consumer.accept(Arrays.asList(
                new Person(21, "zhangsan"),
                new Person(22, "lisi"),
                new Person(23, "wangwu"),
                new Person(24, "wangwu"),
                new Person(23, "lisi"),
                new Person(26, "lisi"),
                new Person(26, "zhangsan")
        ));

        System.out.println(lisiList);

    }

    static class Person {
        private Integer age;
        private String name;

        public Person(Integer age, String name) {
            this.age = age;
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}